package cn.clx.org.controller;

import cn.clx.basic.util.AjaxResult;
import cn.clx.basic.util.PageList;
import cn.clx.org.domain.Deptpartment;
import cn.clx.org.query.DeptpartmentQuery;
import cn.clx.org.service.IDeptpartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptpartmentController {

    @Autowired
    private IDeptpartmentService deptpartmentService;

    //删除
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            System.out.println("jinlaile");
            deptpartmentService.removeById(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("程序错误,请殴打程序员");
        }
    }

    //新增or修改
    @PutMapping
    public AjaxResult updOrsave(@RequestBody Deptpartment deptpartment){
        try {
            if (deptpartment.getId()==null){
                deptpartmentService.save(deptpartment);
            }else {
                deptpartmentService.update(deptpartment);
            }
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("程序错误,请殴打程序员");
        }


    }

    //查询全部
    @GetMapping
    public List<Deptpartment> queryAll(){
        return deptpartmentService.queryAll();
    }

    //id查询
    @GetMapping("/{id}")
    public Deptpartment queryById(@PathVariable("id") Long id){
        return deptpartmentService.queryById(id);
    }


    //高级+分页
    @PostMapping
    public PageList<Deptpartment> queryPage(@RequestBody DeptpartmentQuery query){
        return deptpartmentService.queryPage(query);
    }

    /**
     * 批量删除
     * @param query
     * @return
     */
    @PatchMapping
    public AjaxResult deleteList(@RequestBody DeptpartmentQuery query){
        try {
            deptpartmentService.deleteArray(query);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("程序错误,请殴打程序员");
        }

    }

    /**
     * 无线级树的实现
     * @return
     */
    @GetMapping("/tree")
    public List<Deptpartment> getDeptTree(){
        return deptpartmentService.getDeptTree();
    }



}
